package com.gis507.mapreduce.splitday;

import lombok.SneakyThrows;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author leon
 * @ClassName DayPartition.java
 * @createTime 2021年12月12日 15:38:00
 */
public class DayPartition extends Partitioner<NullWritable, Text> {

    @SneakyThrows
    @Override
    public int getPartition(NullWritable nullWritable, Text text, int i) {
        String comment = text.toString();
        String[] value = comment.split(",");
        Double time = Double.parseDouble(value[10]);

            if (time<DayPartition.getTime("2020-08-03")){
                return 0;
            }else if (time < DayPartition.getTime("2020-08-05")){
                return 1;
            }else if (time < DayPartition.getTime("2020-08-07")){
                return 2;
            }else if (time < DayPartition.getTime("2020-08-09")){
                return 3;
            }else if (time < DayPartition.getTime("2020-08-11")){
                return 4;
            }else if (time < DayPartition.getTime("2020-08-13")){
                return 5;
            }else if (time < DayPartition.getTime("2020-08-15")){
                return 6;
            }else if (time < DayPartition.getTime("2020-08-17")){
                return 7;
            }else if (time < DayPartition.getTime("2020-08-19")){
                return 8;
            }else if (time < DayPartition.getTime("2020-08-21")){
                return 9;
            }else if (time < DayPartition.getTime("2020-08-23")){
                return 10;
            }else if (time < DayPartition.getTime("2020-08-25")){
                return 11;
            }else if (time < DayPartition.getTime("2020-08-27")){
                return 12;
            }else if (time < DayPartition.getTime("2020-08-29")){
                return 13;
            }else{
                return 14;
            }
        }

    public static long getTime(String str) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date time = df.parse(str);
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        long tm = cal.getTimeInMillis();
        return tm/1000;
    }
}
